import {
    AfterContentChecked, AfterViewChecked, AfterViewInit, Component, ElementRef, Input, OnChanges, OnDestroy, OnInit,
    ViewChild
} from '@angular/core';
import {ServerSocket} from '@core/services/ws.service';
import swal from 'sweetalert2';

@Component({
    selector: 'app-c-guess',
    templateUrl: './c-guess.component.html',
    styleUrls: ['./c-guess.component.less']
})
export class CGuessComponent implements OnInit, AfterViewChecked, AfterViewInit, OnDestroy, OnChanges, AfterContentChecked {

    @ViewChild('top') top: ElementRef;
    @ViewChild('center') center: ElementRef;
    @ViewChild('all') all: ElementRef;

    @ViewChild('answerListBox') answerListBox: ElementRef;
    @Input() data;
    @Input() socket;
    @Input() IPPORT;
    @Input() showAnwser = true;
    @Input() goldObj;
    // 测试用
    // time;
    key;
    _answerList = [];

    // 正式时为父组件socket接收数据 @Input()至此组件;
    @Input()
    set answerList(value) {
        this._answerList = value;
        this.center.nativeElement.scrollTop = this.answerListBox.nativeElement.offsetHeight - this.center.nativeElement.offsetHeight;
    }

    get answerList() {
        this.data.fillData.key.push(this._answerList);
        return this._answerList;
    }

    @Input()
    set guesskey(value) {
        //console.log(value)
        if (value) {
            this.data.fillData.key.push(value);
        }

    }

    closePop() {
        this.data.text = false;
    }


    top_one(i) {
        let keyarray = this.data.fillData.key;
        if (i != 0) {
            let key = keyarray[i];
            keyarray.splice(i, 1);
            keyarray.splice(i - 1, 0, key);
            this.data.fillData.key = keyarray;
            //console.log(keyarray);
        }
    }

    down_one(i) {
        let keyarray = this.data.fillData.key;
        if (i != this.data.fillData.key.length - 1) {
            let key = keyarray[i];
            keyarray.splice(i, 1);
            keyarray.splice(i + 1, 0, key);
            //console.log(keyarray);
            this.data.fillData.key = keyarray;
        }
    }

    to_top(i) {
        let keyarray = this.data.fillData.key;
        if (i != 0) {
            let key = keyarray[i];
            keyarray.splice(i, 1);
            keyarray.unshift(key);
            this.data.fillData.key = keyarray;
        }
    }

    to_down(i) {
        let keyarray = this.data.fillData.key;
        if (i != this.data.fillData.key.length - 1) {
            let key = keyarray[i];
            keyarray.splice(i, 1);
            keyarray.push(key);
            // console.log(keyarray);
            this.data.fillData.key = keyarray;
        }
    }

    remove_one(i) {
        if (this.data.fillData.key.length === 1) {
            this.data.fillData.key = [];
        } else {
            this.data.fillData.key.splice(i,1);
        }
    }

    constructor(public ws: ServerSocket, public ref: ElementRef) {
    }

    ngOnInit() {
        this.data.text = false;
    }

    saveKey() {
        if (this.key) {
            if (this.data.fillData.key) {
                this.data.fillData.key.push(this.key);
                this.key = ''
            } else {
                this.data.fillData.key = [this.key];
                this.key = ''
            }
        } else {
            swal(`关键词不能为空或已有关键词`, '', 'info');
        }
    }

    ngAfterViewChecked() {
        // console.log('_________***********');
        // console.log(this.goldObj);
        this.center.nativeElement.scrollTop = this.answerListBox.nativeElement.offsetHeight - this.center.nativeElement.offsetHeight;
    }


    ngAfterViewInit() {
        // if (!this.socket) {
        //     // 测试用
        //     this.time = setInterval(() => {
        //         this.answerList.push({
        //             answer: '回答',
        //             name: 'user'
        //         });
        //         console.log(this.answerListBox.nativeElement.offsetHeight);
        //         this.center.nativeElement.scrollTop = this.answerListBox.nativeElement.offsetHeight - this.center.nativeElement.offsetHeight;
        //     }, 5000);
        // }
    }

    ngOnDestroy() {
        // clearInterval(this.time);
    }

    ngAfterContentChecked() {
        this.center.nativeElement.scrollTop = this.answerListBox.nativeElement.offsetHeight - this.center.nativeElement.offsetHeight;
    }

    ngOnChanges() {

        this.center.nativeElement.scrollTop = this.answerListBox.nativeElement.offsetHeight - this.center.nativeElement.offsetHeight;

    }

}
